/**
 * Created by pankaj on 28/10/14.
 */

define(['jquery','bootstrap.min', 'jquery-ui/jquery-ui','module/form/validate'], function ($, bootstrap, jqueryui, validate) {

    var form = function () {

        var states = [
            'Doctor', 'Charted Accountant', 'Lawyer', 'Software Engineer', 'IT Professional',
            'Entrepreneur', 'Journalist', 'House wife'
        ];

        var validateForm = function () {
            var name = $(".js_update_profile .js_name").val();
            if(validate.empty(name)){
                $(".name_error").show();
                return false;
            }
            var gender = $('.js_gender').val();
            if(gender == -1){
                $(".gender_error").show();
                return false;
            }
            var aboutMe = $(".js_update_profile .js_aboutme").val();
            var phone = $(".js_update_profile .js_phone").val();
            if(validate.empty(phone)){
              //  return false;
            }
            var email = $(".js_update_profile .js_email").val();
            if(!validate.validateEmail(email)){
                return false;
            }
            var birthday = $(".js_update_profile .js_birthday").val();
            if(!validate.date(birthday)){
                //return false;
            }
            var flatNo = $(".js_update_profile .js_flatno").val();
            if(validate.empty(flatNo)){
                $(".flat_error").show();
                return false;
            }
            var profession = $(".js_update_profile .js_profession").val();
            if(validate.empty(profession)){
               // return false;
            }
            var post = $(".js_update_profile .js_post").val();

            return true;
        };

        $(".js_submit").click(function(e){
            //
            if(validateForm()){
                $(".js_update_profile").submit();
            }
            e.preventDefault();
            e.stopPropagation();
        });


        $(".js_update_profile .js_changePhoto").click(function(){
            //trigger file input
            $(".js_update_profile input[type=file]").trigger('click');
        });


        $(".js_update_profile input[type=file]").on('change', function(){
            if (this.files && this.files[0]) {
                $('#profile-upload-preview').css('display', 'block').attr('src', $(this).result);
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('.js_profileimg').attr('src', e.target.result);
                };
                reader.readAsDataURL(this.files[0]);
            }
        });

        $(".js_profession").autocomplete({
            source:states
        });

        $(".js_birthday").datepicker({
            dateFormat : 'MM dd, yy',
            changeYear: true,
            yearRange : "c-100:c+100"
        });

        $(".js_post").autocomplete({
        });
    };
    return form;
});
